<?php
/**
 * JT Library Parameter Class
 *
 * @version 1.0
 * @author Alexey N. Brin (userxp@joomlaportal.ru)
 * @filename sortable.php
 * @package JT.Library
 * @copyright (C) 2008-2009 by joomlatune.ru
 * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
 * 
 * If you fork this to create your own project, 
 * please make a reference to JT.Library someplace in your code 
 * and provide a link to http://www.joomlatune.ru
 *
 */

defined('_VALID_MOS') or defined('_JEXEC') or die('Direct Access to this location is not allowed.');

if( !class_exists( 'JTuneHTMLSortableField' ) ) {

	class JTuneHTMLSortableField
	{
		function _( $name, $label, $prefix, $option='', $adminForm='adminForm' ) {
			//JTuneHTMLSortableField::getInstance($adminForm);
			
			$option = $option ? $option : getUrlParam( 'option' );
			$app = &JTuneConfig::getApplication();
			// 											$_SESSION key, $_REQUEST field, default
			$sort = $app->getUserStateFromRequest( $prefix.$option.$name, $name, '' );
			$class = '';
			if( $sort == 'ASC' || $sort == 'DESC' ) $class = strtolower($sort);
			
			// onclick="sendSorting(\''.$name.'\')"
			$result = '
				<a href="#sort_'.$name.'" class="sorted_link '.$class.'" id="'.$name.'" >
					<span>'.$label.'</span>
					<input type="hidden" name="'.$name.'" value="'.$sort.'" />
				</a>
			';
			return $result;
		} // _
		/*
		function getScript( $adminForm ) {
			ob_start();
				?>
				<script language="javascript" type="text/javascript">
				var sorted_fields = [];
				function sendSorting( fieldname, formname ) {
					if( !formname ) form = document.<?php echo $adminForm; ?>;
					else form = document.getElementsByName(formname);
					eval( 'sortint = form.'+fieldname);
					console.log( sortint );
					switch( sortint.value ) {
						default		: sortint.value='ASC';	break;
						case 'ASC'	: sortint.value='DESC';	break;
						case 'DESC'	: sortint.value='';		break;
					}
					form.submit();
				}
				function clearSorting(fieldname, formname) {
					if( !formname ) form = document.<?php echo $adminForm; ?>;
					else form = document.getElementsByName(formname);
					eval( 'sortint = form.'+fieldname);
				}
				</script>
				<style>
				a.asc span {
					color: blue !important;
				}
				a.desc span {
					color: red !important;
				}
				</style>
				<?php
				$c = ob_get_contents();
			ob_end_clean();
			return $c;
		} //getScript
		*/
		
	} //JTuneHTMLSortableField

}